#include <vector>
#include <unordered_set>

using std::vector;
using std::unordered_set;

class Solution {
public:
    int longestConsecutive(vector<int>& nums) {
        if (nums.size() < 2) {
            return nums.size();
        }
        unordered_set<int> set;
        for (int i : nums) {
            set.insert(i);
        }
        int longest = 0;
        for (int i : set) {
            if (set.count(i - 1) == 0) {
                int num = i;
                int len = 1;
                while (set.count(num + 1) > 0) {
                    num++;
                    len++;
                }
                if (len > longest) {
                    longest = len;
                }
            }
        }
        return longest;
    }
};